package com.bestcem.xm.member.controller;

import com.bestcem.xm.common.core.domain.web.ServiceResult;
import com.bestcem.xm.common.core.domain.web.XmResultJson;
import com.bestcem.xm.common.core.uitls.EnumUtil;
import com.bestcem.xm.common.core.uitls.GRpcUtil;
import com.bestcem.xm.component.security.context.SecurityContextHolder;
import com.bestcem.xm.member.controller.dto.HttpRecordProgressDTO;
import com.bestcem.xm.member.controller.param.export.GetExportProcessRequest;
import com.bestcem.xm.member.enums.MemberTypeEnum;
import com.bestcem.xm.member.grpc.service.param.PageParam;
import com.bestcem.xm.member.grpc.service.param.export.ListExportRecordsParam;
import com.bestcem.xm.member.service.ExportService;
import com.bestcem.xm.member.service.dto.ExportRecordDTO;
import com.bestcem.xm.member.service.dto.PageDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.ConstraintViolation;
import java.util.Set;

/**
 * @author dc.wang <dc.wang@idiaoyan.com>
 * @version V1.0
 * @description 导出控制器
 * @date 2021/8/17
 */
@Slf4j
@RestController
@RequestMapping("/member/v2/export")
public class ExportController {
    private final ExportService exportService;

    @Autowired
    public ExportController(ExportService exportService){
        this.exportService = exportService;
    }

    @GetMapping("/ind/record")
    public XmResultJson<PageDTO<ExportRecordDTO>> listIndExportRecords(@RequestParam(name = "mtype") Integer memberType,
                                                                       @RequestParam(name = "page") Integer page,
                                                                       @RequestParam(name = "rowsPerPage") Integer pageSize,
                                                                       HttpServletRequest request){
        // 依赖用户信息查询
        
        String orgId = SecurityContextHolder.getOrgId();
        
         String userId = SecurityContextHolder.getUserId();
        if (log.isInfoEnabled()) {
            log.info("orgId: {}", orgId);
            log.info("userId: {}", userId);
        }

        MemberTypeEnum memberTypeEnum = EnumUtil.getEnumByIndex(MemberTypeEnum.class, memberType);

        ListExportRecordsParam param = new ListExportRecordsParam();
        param.setOrgId(orgId);
        param.setOpUserId(userId);
        param.setMemberTypeEnum(memberTypeEnum);
        param.setPageParam(new PageParam(page - 1, pageSize));

        // 参数校验
        Set<ConstraintViolation<Object>> violations = GRpcUtil.validateParam(param);
        if (!CollectionUtils.isEmpty(violations)) {
            ConstraintViolation<Object> violation = violations.iterator().next();
            return XmResultJson.fail(-1, violation.getMessage());
        }

        ServiceResult<PageDTO<ExportRecordDTO>> result = this.exportService.listExportRecords(param);
        if(!result.isSuccess()){
            return XmResultJson.fail(-1, result.getMsg());
        }

        return XmResultJson.success(result.getData());
    }

    @GetMapping("/ins/record")
    public XmResultJson<PageDTO<ExportRecordDTO>> listInsExportRecords(@RequestParam(name = "mtype") Integer memberType,
                                                                        @RequestParam(name = "page") Integer page,
                                                                        @RequestParam(name = "rowsPerPage") Integer pageSize,
                                                                        HttpServletRequest request){
        // 依赖用户信息查询
        
        String orgId = SecurityContextHolder.getOrgId();
        
         String userId = SecurityContextHolder.getUserId();
        if (log.isInfoEnabled()) {
            log.info("orgId: {}", orgId);
            log.info("userId: {}", userId);
        }

        MemberTypeEnum memberTypeEnum = EnumUtil.getEnumByIndex(MemberTypeEnum.class, memberType);

        ListExportRecordsParam param = new ListExportRecordsParam();
        param.setOrgId(orgId);
        param.setOpUserId(userId);
        param.setMemberTypeEnum(memberTypeEnum);
        param.setPageParam(new PageParam(page - 1, pageSize));

        // 参数校验
        Set<ConstraintViolation<Object>> violations = GRpcUtil.validateParam(param);
        if (!CollectionUtils.isEmpty(violations)) {
            ConstraintViolation<Object> violation = violations.iterator().next();
            return XmResultJson.fail(-1, violation.getMessage());
        }

        ServiceResult<PageDTO<ExportRecordDTO>> result = this.exportService.listExportRecords(param);
        if(!result.isSuccess()){
            return XmResultJson.fail(-1, result.getMsg());
        }

        return XmResultJson.success(result.getData());
    }

    @GetMapping("/int/record")
    public XmResultJson<PageDTO<ExportRecordDTO>> listIntExportRecords(@RequestParam(name = "mtype") Integer memberType,
                                                                        @RequestParam(name = "page") Integer page,
                                                                        @RequestParam(name = "rowsPerPage") Integer pageSize,
                                                                        HttpServletRequest request){
        // 依赖用户信息查询
        
        String orgId = SecurityContextHolder.getOrgId();
        
         String userId = SecurityContextHolder.getUserId();
        if (log.isInfoEnabled()) {
            log.info("orgId: {}", orgId);
            log.info("userId: {}", userId);
        }

        MemberTypeEnum memberTypeEnum = EnumUtil.getEnumByIndex(MemberTypeEnum.class, memberType);

        ListExportRecordsParam param = new ListExportRecordsParam();
        param.setOrgId(orgId);
        param.setOpUserId(userId);
        param.setMemberTypeEnum(memberTypeEnum);
        param.setPageParam(new PageParam(page - 1, pageSize));

        // 参数校验
        Set<ConstraintViolation<Object>> violations = GRpcUtil.validateParam(param);
        if (!CollectionUtils.isEmpty(violations)) {
            ConstraintViolation<Object> violation = violations.iterator().next();
            return XmResultJson.fail(-1, violation.getMessage());
        }

        ServiceResult<PageDTO<ExportRecordDTO>> result = this.exportService.listExportRecords(param);
        if(!result.isSuccess()){
            return XmResultJson.fail(-1, result.getMsg());
        }

        return XmResultJson.success(result.getData());
    }

//    @DeleteMapping("/records")
//    public XmResultJson<List<ExportRecordDTO>> deleteExportRecords(@RequestParam(name = "mtype") Integer memberType,
//                                                                @RequestParam(name = "page") Integer page,
//                                                                @RequestParam(name = "rowsPerPage") Integer pageSize){
//
//        // 参数校验
//        String orgId = "";
//        MemberTypeEnum memberTypeEnum = EnumUtil.getEnumByIndex(MemberTypeEnum.class, memberType);
//        BatchDeleteExportRecordsParam param = new BatchDeleteExportRecordsParam();
//        param.setOrgId(orgId);
//        param.setIds();
//
//        List<ExportRecord> exportRecords = this.exportService.batchDeleteExportRecords(orgId, userId, memberTypeEnum, offset, pageSize);
//
//        List<ExportRecordDTO> exportRecordDTOS = exportRecords.stream().map(this::convertFrom).collect(Collectors.toList());
//
//        return XmResultJson.success(exportRecordDTOS);
//    }

    @PostMapping("/progress")
    public XmResultJson exportProcess(@RequestBody GetExportProcessRequest request,
                                                 HttpServletRequest httpServletRequest){

        
        String orgId = SecurityContextHolder.getOrgId();
        String userId = SecurityContextHolder.getUserId();
        if (log.isInfoEnabled()) {
            log.info("orgId: {}", orgId);
            log.info("userId: {}", userId);
        }

        // 查询
        ServiceResult<HttpRecordProgressDTO> result = exportService.getExportProgress(request.getId(),  orgId);

        if(!result.isSuccess()){
            return XmResultJson.fail(-1, result.getMsg());
        }
        return XmResultJson.success(result.getData());
    }
}
